package com.zjsru.plan2025.oneday;

/**
 * 43. 字符串相乘
 *
 * @Author: cookLee
 * @Date: 2025-01-05
 */
public class Multiply {

    /**
     * 主
     * \
     * 输入: num1 = "2", num2 = "3"
     * 输出: "6"
     * \
     * 输入: num1 = "123", num2 = "456"
     * 输出: "56088"
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        Multiply multiply = new Multiply();
        String num1 = "123";
        String num2 = "456";
        System.out.println(multiply.multiply2(num1, num2));
    }

    /**
     * 相乘
     *
     * @param num1 num1
     * @param num2 num2
     * @return {@link String}
     */
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        String ans = "0";
        int num1Len = num1.length();
        int num2Len = num2.length();
        for (int i = num2Len - 1; i >= 0; i--) {
            StringBuffer sb = new StringBuffer();
            //进位
            int t = 0;
            //补位
            for (int j = num2Len; j > i; j--) {
                sb.append(0);
            }
            int n2 = num2.charAt(i) - '0';
            for (int j = num1Len - 1; j >= 0; j--) {
                int n1 = num1.charAt(j) - '0';
                //计算当前位
                int sum = n1 * n2 + t;
                sb.append(sum % 10);
                t = sum / 10;
            }
            if (t != 0) {
                sb.append(t % 10);
            }
            //处理此位得出数和已有数相加
            ans = this.helper(ans, sb.reverse().toString());
        }
        return ans;
    }

    private String helper(String num1, String num2) {
        int len1 = num1.length() - 1;
        int len2 = num2.length() - 1;
        //进位
        int t = 0;
        StringBuilder sb = new StringBuilder();
        while (len1 >= 0 || len2 >= 0 || t != 0) {
            int n1 = len1 >= 0 ? num1.charAt(len1) - '0' : 0;
            int n2 = len2 >= 0 ? num2.charAt(len2) - '0' : 0;
            int sum = n1 + n2 + t;
            sb.append(sum % 10);
            t = sum / 10;
            len1--;
            len2--;
        }
        return sb.reverse().toString();
    }


    /**
     * 方法二
     *
     * @param num1 num1
     * @param num2 num2
     * @return {@link String}
     */
    public String multiply2(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int[] result = new int[num1.length() + num2.length()];
        for (int i = num1.length() - 1; i >= 0; i--) {
            for (int j = num2.length() - 1; j >= 0; j--) {
                int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                int p1 = i + j, p2 = i + j + 1;
                int sum = mul + result[p2];
                //分别处理p1进位和p2当前位
                result[p2] = sum % 10;
                result[p1] += sum / 10;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int num : result) {
            if (!(sb.length() == 0 && num == 0)) {
                sb.append(num);
            }
        }
        return sb.length() == 0 ? "0" : sb.toString();
    }

}
